<?php

/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * http://www.gnu.org/copyleft/gpl.html
 */
 


/**
 *  @file
 *  @Author Samuel Maura
 *  create or update wiki page
 *
 *
 */

require_once("".$_SERVER['DOCUMENT_ROOT']."/xenix/controllers/debug.inc");
require_once("".$_SERVER['DOCUMENT_ROOT']."/xenix/controllers/validate.inc");
require_once("".$_SERVER['DOCUMENT_ROOT']."/xenix/lib/session/sessionUtils.inc");

// Create session
session_start();
//Copy post values
savePost();
$_SESSION['validation_error'] = "";
$validatedInputs = array();
$inputs = trimInputArray($_POST);



validateWikiPage($inputs['title'],$inputs['content'],$inputs['group'],$inputs['access']);
checkErrors('validation_error','/xenix/views/wikicreate.php');



// Check again
 if ($_SESSION['validation_error'] == ''){
    require_once("".$_SERVER['DOCUMENT_ROOT']."/xenix/models/entries.php");
    require_once("".$_SERVER['DOCUMENT_ROOT']."/xenix/models/sqlSafe.php");
    require_once("".$_SERVER['DOCUMENT_ROOT']."/xenix/models/user.php");
    
    $entry = new Entries();
    $sqlSafe = new SqlSafe(dbManager::connect());
    
    //var_dump($validatedInputs);
    $validatedInputs['content'] = $sqlSafe->safe($validatedInputs['content']);
    $validatedInputs['title'] = $sqlSafe->safeCustom($validatedInputs['title']);
    $validatedInputs['content'] = strip_tags($validatedInputs['content'],'<p><strong><i><b><br><h2><h1>');
    $validatedInputs['title']   = strip_tags($validatedInputs['title']);
    
   // add metadata
   
    $validatedInputs['user_id'] = $_SESSION['user_id'];
    $validatedInputs['ip'] = $_SERVER['REMOTE_ADDR'];
    
    if (isset($_POST['action']) && ($_POST['action'] == 'edit') ){
      if (isset($_POST['page_id']) && ($_POST['page_id'] !='') && isset($_POST['revision']) && ($_POST['revision'] !='') ){
        $validatedInputs['id'] = $_POST['page_id'];
        $validatedInputs['revision'] = intval($_POST['revision']) + 1;
        $validatedInputs['last_modified'] = date('Y-m-d H:i:s');
        $entry->updateEntry($validatedInputs['id'],$validatedInputs); 
        // Clear form data 
        clearPost();
      }
      else {
        header("Location: /xenix/views/wikierror.php");
        exit;
      }
    }
    else{
      $validatedInputs['id'] = mt_rand(100000,999999).mt_rand(100000,999999);
    
      if ($entry->titleExist($validatedInputs['title'])){
        $_SESSION['validation_error'] = "This title already exist.";
        header("Location: /xenix/views/wikicreate.php");
        exit;
      }
    
      // Save entry
      $entry->add($validatedInputs);
      //Clear form data
      clearPost();
    }
    header("Location: /xenix/views/wikidashboard.php");
 }




function validateWikiPage($title,$content,$group_id,$restriction_id){
  $validate = new Validate();
  
  if ( isset($title) && ($title != '') ){
    $GLOBALS['validatedInputs']['title'] = preg_replace('/[\s]+/','_', $title); 
       
  }
  else{
    // set error message
    $_SESSION['validation_error'] .= "title is required <br/>";
  }
 
  if ( isset($content) && ($content != '') ){
     $GLOBALS['validatedInputs']['content'] = $content;   
  }
  else{
    // set error message
    $_SESSION['validation_error'] .= "content is required <br/>";
  }
  
  if ( isset($group_id) && ($group_id != '') ){
     $GLOBALS['validatedInputs']['group_id'] = $group_id;   
  }
  else{
    // set error message
    $_SESSION['validation_error'] .= "group is required <br/>";
  }
  
  if ( isset($restriction_id) && ($restriction_id != '') ){
     $GLOBALS['validatedInputs']['restriction_id'] = $restriction_id;   
  }
  else{
    // set error message
    $_SESSION['validation_error'] .= "restriction level is required <br/>";
  }
  
} 

// Show debug info
echo $GLOBALS['POST_INPUTS'];

?>